local zongpo = fk.CreateSkill{
  name = "wk_heg__zongpo",
}

Fk:loadTranslationTable{
  ["wk_heg__zongpo"] = "纵迫",
  [":wk_heg__zongpo"] = "每回合限一次，当你于回合外使用牌后，你可交给一名攻击范围内含有你的角色一张手牌，此牌于其手牌区内视为拥有“合纵”标记，然后其可令你获得你使用的牌。",

  ["#wk_heg__zongpo-choose"] = "纵迫：你可以交给一名 攻击范围内含有你的 角色一张手牌",
  ["wk_heg__zongpo_giveback"] = "令 %src 获得其使用的牌",

  ["$wk_heg__zongpo1"] = "得遇曹公，吾之幸也。",
  ["$wk_heg__zongpo2"] = "曹公得荆不喜，喜得吾二人足以。",
}

zongpo:addEffect(fk.CardUseFinished,{
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(zongpo.name) and player.room.current ~= player and player:usedSkillTimes(zongpo.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p) return p:inMyAttackRange(player) end)
    local tos, id = room:askToChooseCardsAndPlayers(player,{
        targets = targets,
        min_card_num = 1,
        max_card_num = 1,
        min_num = 1,
        max_num = 1,
        pattern = ".|.|.|hand|.|.",
        prompt = "#wk_heg__zongpo-choose",
        skill_name = zongpo.name,
        cancelable = true,
    })
    if #tos ~= 0 then
      event:setCostData(self, { tos = tos, cards = id })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    local to = tos[1]
    local cards = event:getCostData(self).cards[1]
    room:obtainCard(to, cards, false, fk.ReasonGive)
    local mark = {}
    table.insert(mark, cards)
    if Fk:getCardById(cards):getMark("@@alliance-public") == 0 then
    room:setCardMark(Fk:getCardById(cards), "@@alliance-public-inhand", 1)
    room:setPlayerMark(to, "wk_heg__zongpo", mark)
    end
    if player.dead or to.dead then return false end
    local choices = {"wk_heg__zongpo_giveback:".. player.id, "Cancel"}
    local choice = room:askToChoice(to,{choices =  choices, skill_name = zongpo.name})
    local card_ids = Card:getIdList(data.card)
    if #card_ids == 0 or choice == "Cancel" then return false end
    if data.card.type == Card.TypeEquip then
      if not table.every(card_ids, function (id)
        return room:getCardArea(id) == Card.PlayerEquip and room:getCardOwner(id) == player
      end) then return false end
    else
      if not table.every(card_ids, function (id)
        return room:getCardArea(id) == Card.Processing
      end) then return false end
    end
    room:moveCardTo(card_ids, Player.Hand, player, fk.ReasonPrey, zongpo.name, nil, false, player.id)
  end,
})

zongpo:addEffect(fk.AfterCardsMove,{
  can_refresh = function(self, event, target, player, data)
    local toLose = {}
    for _, move in ipairs(data) do
      if not move.from then return false end
      if move.from.dead or type(move.from:getMark("wk_heg__zongpo")) ~= "table" then return false end
      local mark = move.from:getMark("wk_heg__zongpo")
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.PlayerHand and table.contains(mark, info.cardId) then
          table.insert(toLose, info.cardId)
        end
      end
    end
    if #toLose > 0 then
      event:setCostData(self, { cards = toLose })
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      local mark = move.from:getMark("wk_heg__zongpo")
      table.forEach(event:getCostData(self).cards, function(id) table.removeOne(mark, id) end)
      table.forEach(event:getCostData(self).cards, function(id) player.room:setCardMark(Fk:getCardById(id), "@@alliance-public-inhand", 0) end)
      player.room:setPlayerMark(move.from, "wk_heg__zongpo", #mark > 0 and mark or 0)
    end
  end,
})

return zongpo